package com.youlu.campus.service.item;

import com.youlu.campus.entity.ActivityBadgeRule;
import com.youlu.campus.entity.ItemInfo;
import com.youlu.campus.entity.VO.req.ItemQueryReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ActivityBadgeRuleService {

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Lazy
    @Autowired
    private ItemInfoService itemInfoService;

    public List<ActivityBadgeRule> findByActivityId(String activityId,Integer badgeType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("itemType").is(badgeType));
        query.addCriteria(Criteria.where("deleted").is(Boolean.FALSE));
        return this.mongoTemplate.find(query, ActivityBadgeRule.class);
    }

    @Async
    public void updateByItemId(ItemInfo updateItemInfo) {
        if (StringUtils.isBlank(updateItemInfo.getId())) {
            return;
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("itemId").is(updateItemInfo.getId()));
        Update update = new Update();
        update.set("itemType", updateItemInfo.getItemType());
        update.set("itemTitle", updateItemInfo.getTitle());
        update.set("price", updateItemInfo.getPrice());
        if(updateItemInfo.getOnSale()){
            update.set("deleted",Boolean.FALSE);
        }else{
            update.set("deleted",Boolean.TRUE);
        }
        update.set("updatedTime", new Date());

        this.mongoTemplate.updateMulti(query, update, ActivityBadgeRule.class);
    }

    public ActivityBadgeRule detail(String id) {
        return this.mongoTemplate.findById(id, ActivityBadgeRule.class);
    }

    public boolean delete(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));

        return this.mongoTemplate.remove(query, ActivityBadgeRule.class).getDeletedCount() > 0 ? true : false;
    }


    public void saveOrUpdate(ActivityBadgeRule activityBadgeRule) {
        Date now = new Date();
        if (StringUtils.isBlank(activityBadgeRule.getId())) {
            activityBadgeRule.setCreatedTime(now);
        }
        activityBadgeRule.setUpdatedTime(now);
        this.mongoTemplate.save(activityBadgeRule);
    }

    public Page<ActivityBadgeRule> list(ItemQueryReq itemQueryReq) {
        Query query = new Query();
        if (StringUtils.isNotBlank(itemQueryReq.getActivityId())) {
            query.addCriteria(Criteria.where("activityId").is(itemQueryReq.getActivityId()));
        }
        PageRequest pageRequest = PageRequest.of(itemQueryReq.getPageNo() - 1, itemQueryReq.getPageSize());
        long count = this.mongoTemplate.count(query, ActivityBadgeRule.class);
        if (count == 0) {
            return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> count);
        }
        query.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<ActivityBadgeRule> list = this.mongoTemplate.find(query.with(pageRequest), ActivityBadgeRule.class);
        list.stream().forEach(activityBadgeRule -> {
            ItemInfo itemInfo = itemInfoService.detail(activityBadgeRule.getItemId());
            if(Objects.nonNull(itemInfo)){
                activityBadgeRule.setPrice(itemInfo.getPrice());
            }
        });
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }
}
